<?php
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/MenuPageEnum.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/FrontController.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/formconverters/PersonSectorInvolvementFormConverter.php';

/**
 * Description of ViewManager
 *
 * @author Reinowin7
 */
class ViewManager 
{
    private $menuPageEnum;
    
    public function navigate($view, $request)
    {  
            $personSectorInvolvementFormConverter = null;
            if (isset($_REQUEST["personSectorInvolvementFormConverter"]))
            {
                $personSectorInvolvementFormConverter = unserialize($_REQUEST["personSectorInvolvementFormConverter"]);
            }
            
            if (isset($_REQUEST["insertingPersonWithSectorInvolvementDataIntoADatabaseWasUnsuccessful"]))
            {
                echo $_REQUEST["insertingPersonWithSectorInvolvementDataIntoADatabaseWasUnsuccessful"];
            }
            
            if (isset($_REQUEST["updatingPersonWithSectorInvolvementDataInADatabaseWasUnsuccessful"]))
            {
                echo $_REQUEST["updatingPersonWithSectorInvolvementDataInADatabaseWasUnsuccessful"];
            }
            
        ?>

        <html lang="et">
          <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Helmes Work Test</title>
          </head>
          <body>
            <div>
                <p>
                    Please enter your name and pick the Sectors you are currently involved in.
                </p>
            </div>
            <div>
                <form action="/HelmesWorkTest/index.php" name="personSectorInvolvementForm" method="post">
                    <input type="hidden" name="formSubmitted" value="insertPersonSectorInvolvementForm"/>
                    <input type="hidden" name="personID" value="<?php 
                                if (!is_null($personSectorInvolvementFormConverter) and !is_null($personSectorInvolvementFormConverter->getPersonID()))
                                {
                                    echo $personSectorInvolvementFormConverter->getPersonID();
                                }
                                else 
                                {
                                    echo "";
                                }
                                ?>"/>
                    <table border="1">
                        <tr>
                            <td>
                                Name:</td><td><input type="text" name="name" value="<?php
                                        if (!is_null($personSectorInvolvementFormConverter) and !is_null($personSectorInvolvementFormConverter->getName()))
                                        {
                                            //echo "". $personSectorInvolvementFormConverter->getName()."";
                                            echo $personSectorInvolvementFormConverter->getName();
                                        }
                                        else
                                        {
                                            echo "";
                                        }
                                        ?>">
                            </td>
                            <td>
                                <?php 
                                    if (!is_null($personSectorInvolvementFormConverter))
                                    {
                                        if (!is_null($personSectorInvolvementFormConverter->getFormatErrors())
                                                and count($personSectorInvolvementFormConverter->getFormatErrors()) > 0)
                                        {
                                            echo $personSectorInvolvementFormConverter->getErrorByField("name");
                                        }
                                    }
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Sectors:
                            </td>
                            <td>
                                    <?php 
                                        $matchFound = "";
                                        if (!is_null($personSectorInvolvementFormConverter) and !is_null($personSectorInvolvementFormConverter->getListOfAllSectors()))
                                        {
                                            echo '<select multiple size="5" name="sectorSelection[]">';
                                            foreach ($personSectorInvolvementFormConverter->getListOfAllSectors() as $sector)
                                            {
                                                if (!is_null($personSectorInvolvementFormConverter->getArrayOfSelectedSectorIDs()))
                                                {
                                                    foreach ($personSectorInvolvementFormConverter->getArrayOfSelectedSectorIDs() as $selectedSectorID)
                                                    {
                                                        if ($sector->getSectorID() == $selectedSectorID)
                                                        {
                                                            echo '<option selected="selected" value="' . $sector->getSectorID() . '">' . $sector->getSectorName() . '</option>';
                                                            $matchFound = "isFound";
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    echo '<option value="' . $sector->getSectorID() . '">' . $sector->getSectorName() . '</option>';
                                                }

                                                if (!is_null($personSectorInvolvementFormConverter) 
                                                        and !is_null($personSectorInvolvementFormConverter->getArrayOfSelectedSectorIDs())
                                                        and $matchFound != 'isFound')
                                                {
                                                    echo '<option value="' . $sector->getSectorID() . '">' . $sector->getSectorName() . '</option>';
                                                }

                                                $matchFound = "";
                                            }
                                            echo '</select>';
                                        }
                                        else
                                        {
                                            echo '<select name="sectorSelection[]"><option value="-1"></option></select>';
                                        }
                                    ?>
                            </td>
                            <td>
                                <?php 
                                    if (!is_null($personSectorInvolvementFormConverter))
                                    {
                                        $formatErrors = $personSectorInvolvementFormConverter->getFormatErrors();
                                        //if (!empty($personSectorInvolvementFormConverter->getFormatErrors()))
                                        if (!empty($formatErrors))
                                        {
                                            echo $personSectorInvolvementFormConverter->getErrorByField("sectorSelection");
                                        }
                                    }
                                ?>
                           </td>
                        </tr>
                        <tr>
                            <?php 
                                if (!is_null($personSectorInvolvementFormConverter) and !is_null($personSectorInvolvementFormConverter->getAgreesToTerms()))
                                {
                                    if ($personSectorInvolvementFormConverter->getAgreesToTerms() == "1")
                                    {
                                        echo '<td><input checked="checked" type="checkbox" name="agreeToTermsCheckbox" value="1"> Agree to terms</td>';
                                    }
                                    else
                                    {
                                        echo '<td><input type="checkbox" name="agreeToTermsCheckbox" value="1"> Agree to terms</td>';
                                    }
                                }
                                else
                                {
                                    echo '<td><input type="checkbox" name="agreeToTermsCheckbox" value="1"> Agree to terms</td>';
                                }
                            ?>
                        </tr>
                        <tr>
                            <td>
                                <input type="submit" value="Save">
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
          </body>
        </html>
     <?php   
    }
       
    /*
        if ($view == "insertPersonSectorInvolvementPage")
        {
            $this->menuPageEnum = MenuPageEnum::INSERT_PERSON_SECTOR_INVOLVEMENT_PAGE; 
        }

        switch ($this->menuPageEnum) 
        {
            case MenuPageEnum::INSERT_PERSON_SECTOR_INVOLVEMENT_PAGE:
                $url = "http://localhost/HelmesWorkTest/index.php";
                $this->redirect_post($url, $request);
                break ;

            default: 
                $url = "http://localhost/HelmesWorkTest/index.php";
                $this->redirect_post($url, $request);
                break ;
        }
    }
    
    public function redirect_post($url, array $data, array $headers = null) 
    {
        $params = array(
            'http' => array(
                'method' => 'POST',
                'content' => http_build_query($data)
            )
        );
        if (!is_null($headers)) 
        {
            $params['http']['header'] = '';
            foreach ($headers as $k => $v) 
            {
                $params['http']['header'] .= "$k: $v\n";
            }
        }
        
        $ctx = stream_context_create($params);
        $fp = @fopen($url, 'rb', false, $ctx);
        
        if ($fp) 
        {
            echo @stream_get_contents($fp);
            die();
        } 
        else 
        {
            // Error
            throw new Exception("Error loading '$url', $php_errormsg");
        }
    }
         * 
         */
}

?>
